package org.example.main.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.main.entity.Admin;
import org.example.main.entity.Tenant;
import org.example.main.utils.CommonResult;
import org.example.main.entity.Landlord;
import org.example.main.service.LandlordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Tag(name = "房东管理",description = "房东管理相关接口")
@RestController
@RequestMapping("/landlord")
public class LandlordController {

    @Autowired
    private LandlordService landlordService;

    @Operation(summary = "房东注册")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "注册成功"),
            @ApiResponse(responseCode = "400", description = "用户名已存在"),
            @ApiResponse(responseCode = "500", description = "注册失败")
    })
    @PostMapping("/register")//http://localhost:8089/main/landlord/register
    public CommonResult register(@RequestBody Landlord landlord) {
        CommonResult commonResult = new CommonResult();
        try{
            Landlord existing = landlordService.findByUsername(landlord.getUsername());
            if (existing != null) {
                commonResult.setCode(400);
                commonResult.setMsg("用户名已存在");
            }else{
                landlordService.add(landlord);
                commonResult.setCode(200);
                commonResult.setMsg("注册成功");
                Landlord current = landlordService.findByUsername(landlord.getUsername());
                commonResult.setData(current);
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("注册失败");
        }
        return commonResult;
    }

    @Operation(summary = "房东登录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "登录成功"),
            @ApiResponse(responseCode = "401", description = "用户名或密码错误")
    })
    @PostMapping("/login")//http://localhost:8089/main/landlord/login
    public CommonResult login(@RequestBody Landlord landlord) {
        CommonResult commonResult = new CommonResult();
        try{
            Landlord real = landlordService.findByUsername(landlord.getUsername());
            if (real == null || !real.getPassword().equals(landlord.getPassword())) {
                commonResult.setCode(401);
                commonResult.setMsg("用户名或密码错误");
            } else {
                commonResult.setCode(200);
                commonResult.setMsg("登录成功");
                commonResult.setData(real);
            }
        }catch(Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("登陆失败");
        }
        return commonResult;
    }

    @Operation(summary = "删除房东记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "删除成功"),
            @ApiResponse(responseCode = "500",description = "删除失败")
    })
    @DeleteMapping("/delete/{landlordId}")//http://localhost:8089/main/landlord/delete/{adminId}
    public CommonResult deleteById(@PathVariable long landlordId) {
        CommonResult commonResult = new CommonResult();
        try{
            landlordService.deleteById(landlordId);
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("删除失败");
        }
        return commonResult;
    }

    @Operation(summary = "修改房东记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "修改成功"),
            @ApiResponse(responseCode = "500",description = "修改失败")
    })
    //修改数据
    @PutMapping("/updateById")//http://localhost:8089/main/landlord/updateById
    public CommonResult updateById(@RequestBody Landlord landlord) {
        CommonResult commonResult = new CommonResult();
        try {
            landlordService.updateById(landlord);
            commonResult.setCode(200);
            commonResult.setMsg("修改成功");
        }
        catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("修改失败"+e.getMessage());
        }
        return commonResult;
    }

    @Operation(summary = "查询所有")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询错误")
    })
    @GetMapping("/findAll")//http://localhost:8089/main/landlord/findAll
    public CommonResult findAll() {
        CommonResult commonResult = new CommonResult();
        try{
            List<Landlord> landlords = landlordService.findAll();
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(landlords);
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "按id查询")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "查询成功"),
            @ApiResponse(responseCode = "500",description = "查询失败"),
            @ApiResponse(responseCode = "404",description = "未查询到")
    })
    @GetMapping("/findById/{landlordId}")//http://localhost:8089/main/landlord/findById/{landlordId}
    public CommonResult findById(@PathVariable long landlordId) {
        CommonResult commonResult = new CommonResult();
        try{
            Landlord landlord=landlordService.findById(landlordId);
            if(landlord!=null){
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(landlord);
            }
            else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }
}

